package chess;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.io.FileInputStream;
import sun.audio.*;

//本人Gitee主页：https://gitee.com/mimocvb
public class Board extends JPanel implements MouseListener,MouseMotionListener{

	private static final long serialVersionUID = 1L;
	static final int MARGIN = 50;
	static final int GRID_SPAN = 45;
	static final int ROWS = 14;
	static final int COLS = 14;
	
	//各个棋盘以及棋子的皮肤资源读入
    private static Image image1_1 = (Image) new ImageIcon("file/A_background.jpg").getImage();
    private static Image image1_2 = (Image) new ImageIcon("file/B_background.jpg").getImage();
    private static Image image1_3 = (Image) new ImageIcon("file/C_background.jpg").getImage();
    static Image image2_1 = (Image) new ImageIcon("file/A_black.png").getImage();
    static Image image2_2 = (Image) new ImageIcon("file/A_withe.png").getImage();
    static Image image3_1 = (Image) new ImageIcon("file/B_black.png").getImage();
    static Image image3_2 = (Image) new ImageIcon("file/B_withe.png").getImage();
    static Image image4_1 = (Image) new ImageIcon("file/C_black.png").getImage();
    static Image image4_2 = (Image) new ImageIcon("file/C_withe.png").getImage();
    static Image image_mouse = (Image) new ImageIcon("file/mouse_zy.png").getImage();
    
    //当前面板皮肤
    static int style = 2;

    //预选框参数定义
    static int mouse_z = 0,mouse_y = 0;
    static int ax = 0;
    static int ay = 0;
    
	FileInputStream fis = null;	
	
	static Point []chessList = new Point[(ROWS+1)*(COLS+1)];
	boolean isBlack = true;
	boolean gameover = false;
	static int chessCount = 0;
	int xindex, yindex;
	
	
	public Board ()
	{
		 
		addMouseListener(this);
		addMouseMotionListener(this);
		
	}
	
	@SuppressWarnings("restriction")
	public void paintComponent (Graphics g) 
	{
		super.paintComponent(g);// 是蛮重要哦
		
        //设置背景图
		if(style == 1){
			g.drawImage(image1_1, 0, 0, this.getWidth(), this.getHeight(), this);
		}
		else if(style == 2){
			g.drawImage(image1_2, 0, 0, this.getWidth(), this.getHeight(), this);
		}
		else if(style == 3){
			g.drawImage(image1_3, 0, 0, this.getWidth(), this.getHeight(), this);
		}
		
		
		//绘制棋盘网格线
		for (int i = 0; i <= ROWS; i++)
		{
			g.drawLine(MARGIN, MARGIN + i*GRID_SPAN, MARGIN + COLS*GRID_SPAN, MARGIN + i*GRID_SPAN);
		}
		for (int i = 0; i <= COLS; i++)
		{
			g.drawLine(MARGIN + i*GRID_SPAN, MARGIN, MARGIN + i*GRID_SPAN, MARGIN + ROWS*GRID_SPAN);
		}
		
		
		//绘制棋子
		for (int i = 0; i < chessCount; i ++)
		{
			//坐标转换
			int xpos = chessList[i].getX() *GRID_SPAN + MARGIN;
			int ypos = chessList[i].getY() *GRID_SPAN + MARGIN;

			
				//判断棋盘皮肤
				switch(style){
				case 1:
					if(chessList[i].getColor() == Color.BLACK){
						g.drawImage(image2_1, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					}
					else g.drawImage(image2_2, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					break;
				case 2:
					if(chessList[i].getColor() == Color.BLACK){
						g.drawImage(image3_1, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					}
					else g.drawImage(image3_2, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					break;
				case 3:
					if(chessList[i].getColor() == Color.BLACK){
						g.drawImage(image4_1, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					}
					else g.drawImage(image4_2, xpos - Point.DIAMETER / 2-2, ypos - Point.DIAMETER / 2-2, 35, 35, this);
					break;
				
				}
				
				if(mouse_y == 0){
					
					try {
						FileInputStream fis = new FileInputStream("file/chessmusic.wav");
						AudioStream as=new AudioStream(fis);
						AudioPlayer.player.start(as);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					mouse_y = 1;
					
				}
				
				//绘制下棋标志
				if (i == chessCount - 1 )
				{
					if(mouse_z==1){
						
						ax = (ax*GRID_SPAN + MARGIN) - Point.DIAMETER / 2-2;
						ay = (ay*GRID_SPAN + MARGIN) - Point.DIAMETER / 2-2;
						
						if(ax < 700 && ay < 700) g.drawImage(image_mouse, ax, ay, 35, 35, this);
						
						mouse_z = 0;
						
					}
					else{
						//下棋音效
						try {
							FileInputStream fis = new FileInputStream("file/chessmusic.wav");
							AudioStream as=new AudioStream(fis);
							AudioPlayer.player.start(as);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
			    	}
	
					g.setColor(Color.RED);
					g.fillOval(xpos - Point.DIAMETER / 2+13,ypos - Point.DIAMETER / 2 +13, 5, 5);
				}
		}
	}

	
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if (gameover)
			return ;
		String colorName = isBlack ? "黑棋" : "白棋" ;
		xindex = (e.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		yindex = (e.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		if (xindex < 0 || xindex > ROWS || yindex < 0 || yindex > COLS)
			return;
		if (findChess(xindex, yindex))
			return;
		
		Point ch = new Point(xindex, yindex, isBlack ? Color.black : Color.white);
		chessList[chessCount++] = ch;
		repaint();
		if (isWin())
		{
			String msg = String.format("恭喜，%s赢啦！", colorName);
			JOptionPane.showMessageDialog(this, msg);// 不是很懂
			gameover = true;
			restartGame ();
			
		}
		isBlack = !isBlack; //why
	}
	
	private boolean findChess(int xindex2, int yindex2) {
		// TODO Auto-generated method stub
		for (Point c : chessList)
			if (c != null && c.getX() == xindex2 && c.getY() == yindex2)
				return true;
		return false;
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
	
	public void style(){
		
		if(style == 1){
			style = 2;
			repaint();
			
		}
		else if(style == 2){
			style = 3;
			repaint();
		}
		else if(style == 3){
			style = 1;
			repaint();
		}
	}
	
	private boolean isWin() {
		// TODO Auto-generated method stub
		int continueCount = 1;
		for (int i = xindex - 1; i >= 0; i--)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(i , yindex, c) != null)
				continueCount++;
			else
				break;
		}
		
		for (int i = xindex + 1; i <= ROWS; i++)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(i , yindex, c) != null)
				continueCount++;
			else
				break;
		}
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;
		
		for (int y = yindex - 1; y >= 0; y--)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(xindex, y, c) != null)
				continueCount++;
			else
				break;
		}
		
		for (int y = yindex + 1; y <= ROWS; y++)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(xindex, y, c) != null)
				continueCount++;
			else
				break;
		}
		
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;
		
		for (int x = xindex + 1, y = yindex - 1; y >= 0 && x <= ROWS; x++, y--)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		for (int x = xindex - 1, y = yindex + 1; y <= COLS && x >= 0; x--, y++)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;
		
		for (int x = xindex - 1, y = yindex - 1; y >= 0 && x >= 0; x--, y--)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		
		for (int x = xindex + 1, y = yindex + 1; y <= COLS && x <= ROWS; x++, y++)
		{
			Color c = isBlack ? Color.black : Color.white;
			if (getChess(x, y, c) != null)
				continueCount++;
			else
				break;
		}
		
		if (continueCount >= 5)
			return true;
		else
			continueCount = 1;
			
		return false;
	}
	
	private Point getChess (int x, int y, Color color)
	{
		for (Point c : chessList)
		{
			if (c != null && c.getX() == x && c.getY() == y && c.getColor() == color)
				return c;
		}
		return null;
	}
	
	public void restartGame ()
	{
		for (int i = 0; i < chessList.length; i++)
			chessList[i] = null;
		isBlack = true;
		gameover = false;
		chessCount = 0;
		mouse_y = 0;
		repaint();		
	}
	public void goback() {
		// TODO Auto-generated method stub
		if (chessCount == 0)
			return ;
		chessList[chessCount - 1] = null;
		chessCount--;
		if (chessCount > 0)
		{
			xindex = chessList[chessCount - 1].getX();
			yindex = chessList[chessCount - 1].getY();
		}
		isBlack = !isBlack;
		repaint();
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
		ax = (arg0.getX() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		ay = (arg0.getY() - MARGIN + GRID_SPAN / 2) / GRID_SPAN;
		
		mouse_z = 1;
		
		repaint();
		
	}
}
